#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

int n, m;
int dp[105][105];
struct Edge {
  int v, w;
};
vector<Edge> ch[105];
void dfs(int u, int fu) {
  for (Edge e : ch[u]) {
    int v = e.v;
    if (v == fu) continue;
    dfs(v, u);
    per(c, m, 1) {
      rep(d, 0, c - 1) {
        dp[u][c] = max(dp[u][c], dp[u][c - 1 - d] + e.w + dp[v][d]);
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  rep(i, 2, n) {
    int u, v, w;
    cin >> u >> v >> w;
    ch[u].push_back(Edge{v, w});
    ch[v].push_back(Edge{u, w});
  }
  dfs(1, 1);
  cout << dp[1][m];
  return 0;
}